# -*- coding: utf-8 -*-
import cgi
import datetime
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp

import views.MP as MPv
import models.milestonePlan as MPm
import models.user as um
import decorator as dc

from models.enhancedModel import EnhancedModel

class MPContr( webapp.RequestHandler ): 
    def debug_self(self, what, action, params, kwds): #debug
        self.response.out.write("(%s)" % what)
        self.response.out.write("\n<br>" + action )
        for (i,p) in enumerate(params):
            self.response.out.write("\n<br>param %d = %s" % (i,p) )
        self.response.out.write("\n<br>kwds=%s" % str(kwds) )
        self.response.out.write("\n<br>url=%s" % ( self.request.url ) )
        self.response.out.write("\n<br>args=%s" % ( self.request.arguments() ) )
        for name in self.request.arguments():
            self.response.out.write("\n<br>arg: <b>%s</b>=%s" % ( name, self.request.get(name) ) )

        self.response.out.write( repr(type(MPv)) )   ; self.response.out.write("\n<br>")
        self.response.out.write( repr(MPv.__name__) ); self.response.out.write("\n<br>")
        self.response.out.write( repr(MPv.__path__) ); self.response.out.write("\n<br>")
        self.response.out.write( repr(MPv.__doc__) ) ; self.response.out.write("\n<br>")
        self.response.out.write( repr(MPv.__file__) ); self.response.out.write("\n<br>")
        self.response.out.write( str((type(MPv),dir(MPv)))) ; self.response.out.write("\n<br>")

    @dc.decorate
    def get(self, action, *params, **kwds):
        permissionName = {"show1" : "mpShow",
                       "showTemplate" : "mpTemp"
                       }

        if not um.UserModel.currentHasPermission(permissionName[action]):
          self.response.out.write("<font color=\"red\"><b>You have no rights to do this action.</b></font>")
          return

        #self.debug_self('get', action, params, kwds)
            
        if action == 'show1': # show1/id/number
            showArchival = self.request.get('archival') == 'True'
        
            if showArchival:
                q = EnhancedModel.queryAll( MPm.MPModel, int(params[0])) 
            else:
                q = EnhancedModel.queryLatest( MPm.MPModel, int(params[0]))
				
            for mp in q:
                self.response.out.write(MPv.show1( mp ))

        elif action == 'showTemplate': # showTemplate
            # tu trzeba miec najpierw import template'ow
            # czy to jest wogole potrzebne?
            self.response.out.write(MPv.showTemplate( ))


